package com.zhs.gccs.view.gt;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


import com.zhs.gccs.R;
import com.zhs.gccs.model.CheckBoxModel;
import com.zhs.gccs.view.BaseView;

import java.util.ArrayList;
import java.util.List;

public class QueryItemView extends BaseView {
    private View ll_item;
    private TextView tv_title;
    private RecyclerView rv_items;
    private List<CheckBoxModel> items;
    private QueryItemAdapter adapter;
    private String title;

    public QueryItemView(@NonNull Context context) {
        super(context);
    }

    public QueryItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public QueryItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_query_item,
                this, true);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.QueryItemView);

        title = typedArray.getString(R.styleable.QueryItemView_title);
        int col_count = typedArray.getInteger(R.styleable.QueryItemView_col_count,3);
        typedArray.recycle();
        ll_item = view.findViewById(R.id.ll_item);
        tv_title = view.findViewById(R.id.tv_title);
        tv_title.setText(title);
        rv_items = view.findViewById(R.id.rv_items);
        this.items = new ArrayList<>();
        rv_items.setLayoutManager(new StaggeredGridLayoutManager(col_count, StaggeredGridLayoutManager.VERTICAL));
        adapter = new QueryItemAdapter(context, items);
        rv_items.setAdapter(adapter);
        rv_items.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if(onClickListener!=null)onClickListener.onClick();
            }
        });
        adapter.setOperationListener(new QueryItemAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                if(onClickListener!=null)onClickListener.onClick();
            }
        });
        ll_item.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                if(onClickListener!=null)onClickListener.onClick();
            }
        });
    }

    public void setData(List<CheckBoxModel> data){
        this.items.clear();
        if(data!=null){
            this.items.addAll(data);
        }
        adapter.notifyDataSetChanged();
    }

    public OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    OnClickListener onClickListener;
    public interface OnClickListener {
        void onClick();
    }
}
